package com.fds.system.controller;

import com.fds.system.service.UserService;
import com.fds.system.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(tags="测试接口")
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private UserService userService;

    /**
     * 自定义登录
     * @return
     */
    @ApiOperation("登录接口")
    @GetMapping(value = "login")
    public String login(String username, String password) {
        return userService.login(username, password);
    }

    @ApiOperation("权限测试接口")
    @GetMapping("test1")
    @PreAuthorize("hasAuthority('test:add')")
    public String test1() {
        log.info(CommonUtils.getLoginUser().toString());
        return "登录成功访问页面";
    }

    @ApiOperation("测试接口报错")
    @GetMapping("test2")
    @PreAuthorize("hasAuthority('test:add')")
    public String test2() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        authentication.getCredentials().toString();
        return "登录成功访问页面";
    }

    @ApiOperation("测试没有权限接口")
    @GetMapping("test3")
    @PreAuthorize("hasAuthority('test:add1')")
    public String test3() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return "登录成功访问页面";
    }

    @ApiOperation("测试登录就可以访问")
    @GetMapping("test4")
    public String test4() {
        return "我可以直接访问";
    }
}
